package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.aspect.annotation.SaveCommonFieldAnno;
import com.sky.aspect.annotation.UpdateCommonFieldAnno;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.vo.DishItemVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SetmealMapper {

    /**
     * 根据分类id查询套餐的数量
     * @param id
     * @return
     */
    @Select("select count(id) from setmeal where category_id = #{categoryId}")
    Integer countByCategoryId(Long id);

    /**
     * 根据套餐名从套餐表查询套餐信息
     * @param name
     * @return
     */
    @Select("select id, category_id, name, price, status, description, image, create_time, update_time, create_user, update_user from setmeal " +
            "where name = #{name}")
    Setmeal getByName(String name);

    /**
     * 分页查询套餐基础表，条件有状态，套餐名称（模糊），分类
     * @param dto
     * @return
     */
    Page<Setmeal> page(SetmealPageQueryDTO dto);

    /**
     * 根据id从套餐表查询套餐信息
     * @param id
     * @return
     */
    @Select("select id, category_id, name, price, status, description, image, create_time, update_time, create_user, update_user from setmeal " +
            "where id = #{id}")
    Setmeal getById(Long id);

    /**
     * 添加一条套餐基本信息到套餐表
     * @param setmeal
     */
    @SaveCommonFieldAnno
    @Insert("insert setmeal(category_id, name, price, status, description, image, create_time, update_time, create_user, update_user) " +
            "values (#{categoryId}, #{name}, #{price}, #{status}, #{description}, #{image}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser});")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void save(Setmeal setmeal);

    /**
     * 根据id从套餐基本信息表删除套餐信息
     * @param id
     */
    @Delete("delete from setmeal where id=#{id}")
    void delById(long id);

    /**
     * 根据id更新套餐表的套餐信息
     * @param setmeal
     */
    @UpdateCommonFieldAnno
    void updateById(Setmeal setmeal);

    /**
     * 动态条件查询套餐
     * @param setmeal
     * @return
     */
    List<Setmeal> list(Setmeal setmeal);

    /**
     * 根据套餐id查询菜品选项
     * @param setmealId
     * @return
     */
    @Select("select sd.name, sd.copies, d.image, d.description " +
            "from setmeal_dish sd left join dish d on sd.dish_id = d.id " +
            "where sd.setmeal_id = #{setmealId}")
    List<DishItemVO> getDishItemBySetmealId(Long setmealId);
}
